<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>微博首页</h1>
<div>
    <div v-if="!isLogin">
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <div v-if="isLogin">
        <h3>欢迎{{user.nick}}回来</h3>
        <a href="/insert.html">发微博</a>
        <a href="javascript:void(0)" @click="logout()">登出</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3><a :href="'/detail.html?id='+weibo.id">
            {{weibo.author}}说:{{weibo.title}}</a></h3>
        <img :src="weibo.url" style="width: 200px" alt="">
    </div>
</div>

<!--引入CDN服务器的框架文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js"></script>

<script>
    let v = new Vue({
        el:"body>div",
        data:{
            isLogin:false,
            user:{},
            arr:[]
        },
        created:function () {
            axios.get("/currentUser").then(function (response) {
                v.user= response.data;
               v.isLogin = v.user==""?false:true;
            })
            //发请求获取所有微博数据
            axios.get("/weibo/select").then(function (response) {
                v.arr = response.data;
            })
        },
        methods:{
            logout:function () {
                if (confirm("您确认登出吗?")){
                    axios.get("/logout").then(function () {
                        location.reload();
                    })
                }
            }
        }
    })
</script>
</body>
</html>